#include "ammmanager.h"
#include "util/amutils.h"

#define KEY_BASE "model-%1"

AMMeshManager * AMMeshManager::get() {
    static AMMeshManager * theInstance = new AMMeshManager;
    return theInstance;
}

AMMeshManager::AMMeshManager(QObject *parent) : QObject(parent)
{
    m_Count = 0;
    m_WFImporter = new WFImporter(this);
    m_AMImporter = new AMImporter(this);
    m_AMExporter = new AMExporter(this);
}

AMMeshManager::~AMMeshManager()
{
    foreach(const QString & str, m_MeshData.keys()) {
        MeshData * m = m_MeshData[str];
        AMUtils::freeMeshData(m);
    }
}

MeshData * AMMeshManager::getMesh(const QString & meshName) {
    return m_MeshData[meshName];
}

void AMMeshManager::importOBJ(const QString & filename)
{
    MeshData * m = m_WFImporter->read(filename);
    m_Count++;
    QString lookup = QString(KEY_BASE).arg(m_Count);
    m_MeshData[lookup] = m;
    emit(meshLoaded(lookup, m));
}

void AMMeshManager::importAM(const QString & filename)
{
    MeshData * m = m_AMImporter->read(filename);
    m_Count++;
    QString lookup = QString(KEY_BASE).arg(m_Count);
    m_MeshData[lookup] = m;
    emit(meshLoaded(lookup, m));
}

void AMMeshManager::exportAM(const QString & meshName, const QString & filename)
{
    MeshData * m = m_MeshData[meshName];
    m_AMExporter->write(filename, m);
}

void AMMeshManager::exportOBJ(const QString & meshName, const QString & filename)
{

}
